package manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import core.constants.Constant;
import core.domain.ListDataInfo;
import core.enums.ResultCode;
import core.exception.ServiceException;
import manage.domain.question.Question;
import manage.domain.question.dto.QuestionAddDTO;
import manage.domain.question.dto.QuestionEditDTO;
import manage.domain.question.dto.QuestionQueryDTO;
import manage.domain.question.es.QuestionES;
import manage.domain.question.vo.QuestionDetailVO;
import manage.domain.question.vo.QuestionVO;
import manage.es.QuestionRepository;
import manage.manager.QuestionCacheManage;
import manage.mapper.QuestionMapper;
import manage.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RefreshScope
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionCacheManage questionCacheManage;
    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        String excludeIdStr = questionQueryDTO.getExcludeStr();
        if(StrUtil.isNotEmpty(excludeIdStr)) {
            String[] excludeIdArr = excludeIdStr.split(Constant.SPLIT_SEM);
            Set<Long> excludeIdSet = Arrays.stream(excludeIdArr)
                    .map(Long::parseLong)
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        return questionMapper.selectQuestionList(questionQueryDTO);
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        Question question = questionMapper
                .selectOne(new QueryWrapper<Question>()
                        .eq("title", questionAddDTO.getTitle()));
        if(question != null) {
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question newQuestion = new Question();
        BeanUtil.copyProperties(questionAddDTO, newQuestion);
        int insert = questionMapper.insert(newQuestion);
        if (insert <= 0){
            return false;
        }
        newQuestion.setQuestionId(questionMapper.selectOne(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle())
                .select(Question::getQuestionId)).getQuestionId());
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(newQuestion, questionES);
        questionRepository.save(questionES);
        questionCacheManage.addCache(newQuestion.getQuestionId());
        return true;
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question, questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question question = questionMapper.selectById(questionEditDTO.getQuestionId());
        if(question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
//        BeanUtil.copyProperties(questionEditDTO, question);
        question.setTitle(questionEditDTO.getTitle());
        question.setDifficulty(questionEditDTO.getDifficulty());
        question.setTimeLimit(questionEditDTO.getTimeLimit());
        question.setSpaceLimit(questionEditDTO.getSpaceLimit());
        question.setContent(questionEditDTO.getContent());
        question.setQuestionCase(questionEditDTO.getQuestionCase());
        question.setDefaultCode(questionEditDTO.getDefaultCode());
        question.setMainFunc(questionEditDTO.getMainFunc());
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question, questionES);
        questionRepository.save(questionES);
        return questionMapper.updateById(question);
    }

    @Override
    public int delete(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        questionRepository.deleteById(questionId);
        questionCacheManage.removeCache(questionId);
        return questionMapper.deleteById(questionId);
    }
}
